import tkinter as tk
import pickle
import tkinter.messagebox
root = tk.Tk()
root.title('welcome to Mofan Python')
root.geometry('450x320')

c = tk.Canvas(root,width=420,height=200)
open_photo = tk.PhotoImage(file=r'C:\Users\Administrator\Desktop\22.gif')
photo = c.create_image(0,130,anchor='w',image=open_photo)
c.pack(side='top')

tk.Label(root,text='User name :').place(x=70,y=220)
tk.Label(root,text='Password :').place(x=70,y=250)

var_use_name = tk.StringVar()
var_use_name.set('python@DJY.com')
var_use_pwd = tk.StringVar()
e1 = tk.Entry(root,textvariable=var_use_name,width=26)
e1.place(x=180,y=221)
e2 = tk.Entry(root,textvariable=var_use_pwd,width=26,show='*')
e2.place(x=180,y=251)

def usr_login():
    usr_name = var_use_name.get()
    usr_pwd = var_use_pwd.get()
    try:
        with open(r'C:\Users\Administrator\Desktop\djy.pickle','rb') as usr_file:
            usr_info = pickle.load(usr_file)
    except:
        with open(r'C:\Users\Administrator\Desktop\djy.pickle','wb') as usr_file:
            usr_info = {'admin':'admin'}
            pickle.dump(usr_info,usr_file)
    if usr_name in usr_info:
        if usr_pwd == usr_info[usr_name]:
            tk.messagebox.showinfo(title='Welcome',message='How are you?' + usr_name)
        else:
            tk.messagebox.showerror(message='Error,you password is wrong,try again')
    else:
        is_sign_up = tk.messagebox.askyesno(message='Welcome, you have not sign up yet.Sign up today?')
        if is_sign_up:
            usr_sign_up()
def usr_sign_up():
    def usr_login_root_2_1():
        np_1 = var_use_name_2.get()
        np_2 = var_use_pwd_2.get()
        np_3 = var_use_pwd_con_1.get()
        with open(r'C:\Users\Administrator\Desktop\djy.pickle','rb') as usr_file:
            usr_info_2 = pickle.load(usr_file)
        if np_2 != np_3:
            tk.messagebox.showwarning(message='两次密码不一样')
        elif np_1 in usr_info_2:
            tk.messagebox.showerror(message='用户名已存在')
        else:
            usr_info_2[np_1] = np_2
            with open(r'C:\Users\Administrator\Desktop\djy.pickle', 'wb') as usr_file:
                pickle.dump(usr_info_2,usr_file)
            tk.messagebox.showinfo(message='Welcome,注册成功！')
            root_2.destroy()

    root_2 = tk.Toplevel(root)
    root_2.geometry('350x200')
    root_2.title('Sign up window')

    tk.Label(root_2, text='User name :').place(x=20, y=30)
    tk.Label(root_2, text='Password :').place(x=20, y=75)
    tk.Label(root_2, text='Confirm password :').place(x=20, y=120)

    var_use_name_2 = tk.StringVar()
    var_use_pwd_2 = tk.StringVar()
    var_use_pwd_con_1 = tk.StringVar()
    e3 = tk.Entry(root_2, textvariable=var_use_name_2, width=26)
    e3.place(x=120, y=31)
    e4 = tk.Entry(root_2, textvariable=var_use_pwd_2, width=26, show='')
    e4.place(x=120, y=76)
    e5 = tk.Entry(root_2, textvariable=var_use_pwd_con_1, width=20, show='')
    e5.place(x=163, y=122)

    b3 = tk.Button(root_2, text='注册', command=usr_login_root_2_1)
    b3.place(x=270, y=153)



b1 = tk.Button(root,text='登陆立即下单',command=usr_login)
b1.place(x=180,y=280)
b2 = tk.Button(root,text='注册立即下单',command=usr_sign_up)
b2.place(x=280,y=280)


root.mainloop()